package cn.yangliu.ui;

import cn.yangliu.tools.Constans;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Vector;

public class MappingUI extends JDialog {
    private JTable table;
    private JPanel contentPanel;
    private JButton sureButton;

    private static Vector<String> header = new Vector<>(2);

    private static Vector<Vector<String>> rows = new Vector<>();


    static {
        Enumeration<?> enumeration = Constans.mysql_mapping.propertyNames();
        while (enumeration.hasMoreElements()) {
            Vector<String> dataLine = new Vector<>();
            Object data = enumeration.nextElement();
            String dbType = data.toString();
            dataLine.add(dbType);

            String javaType = Constans.mysql_mapping.getProperty(dbType);
            dataLine.add(javaType);
            rows.add(dataLine);
        }

        rows.sort(Comparator.comparing(o -> o.get(0)));

        header.add("数据库类型");
        header.add("java类型");
    }

    private static final int WINDOW_WIDTH = 500;
    private static final int WINDOW_HEIGHT = 600;

    public MappingUI() {
        init();
        addActionListener();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int width = toolkit.getScreenSize().width;
        int height = toolkit.getScreenSize().height;
        int x = (width - WINDOW_WIDTH) / 2;
        int y = (height - WINDOW_HEIGHT) / 2;
        setLocation(x, y);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        Dimension dimension = new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
        setMaximumSize(dimension);
        setMinimumSize(dimension);

        table.updateUI();
        contentPanel.updateUI();
    }

    private void addActionListener() {
        sureButton.addActionListener(e -> {
            if (this.isShowing()) {
                setVisible(false);
            }

            int rowCount = table.getRowCount();


            for (int i = 0; i < rowCount; i++) {
                String dbType = table.getValueAt(i, 0).toString();
                String javaType = table.getValueAt(i, 1).toString();
                Constans.custome_mapping.put(dbType, javaType);
            }

        });
    }


    private void init() {
        contentPanel.add(table.getTableHeader(), BorderLayout.NORTH);
        // 把 表格内容 添加到容器中心
        contentPanel.add(table, BorderLayout.CENTER);
        table.setVisible(true);


        // 创建一个表格，指定 所有行数据 和 表头
        DefaultTableModel defaultTableModel = new DefaultTableModel(rows, header);
        table.setModel(defaultTableModel);

        // 把 表头 添加到容器顶部（使用普通的中间容器添加表格时，表头 和 内容 需要分开添加）
        contentPanel.add(table.getTableHeader(), BorderLayout.NORTH); // 把 表格内容 添加到容器中心
        contentPanel.add(table, BorderLayout.CENTER);
        setContentPane(contentPanel);
    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout(0, 0));
        table = new JTable();
        contentPanel.add(table, BorderLayout.CENTER);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        contentPanel.add(panel1, BorderLayout.SOUTH);
        sureButton = new JButton();
        sureButton.setText("确定");
        panel1.add(sureButton);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPanel;
    }
}
